#include "Playlist.h"

using namespace std;

/*---------------------------------------------------------------------------------------
--	FUNCTION:	Playlist::Playlist
--
--	DATE:			March 22, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Brad Paugh
--
--	PROGRAMMER:		Brad Paugh
--
--      INTERFACE:      Playlist::Playlist()
--
--	RETURNS:		
--
--	NOTES:
--		default constructor for playlist
---------------------------------------------------------------------------------------*/
Playlist::Playlist()
{
}

/*---------------------------------------------------------------------------------------
--	FUNCTION:	Playlist::addSong
--
--	DATE:			March 22, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Brad Paugh
--
--	PROGRAMMER:		Brad Paugh
--
--  INTERFACE:      void addSong(Song s)
--						Song s - the song to add to the playlist
--
--	RETURNS:		
--
--	NOTES:
--		adds a song to the playlist. If the song is already in the playlist it will not
--		add another instance of it, however it will set the votes to whatever the votes
--		in that song are.
---------------------------------------------------------------------------------------*/
void Playlist::addSong(Song s)
{
	//check to make sure song does not already exist in playlist
	list<Song>::iterator it;

	for (it = playlist_.begin(); it != playlist_.end(); it++)
	{
		if ((*it).getFilename() == s.getFilename())
		{
			//we still want to set the votes even if it is in playlist
			(*it).setVotes(s.getVotes());
			return;
		}
	}

	playlist_.push_back(s);
	notifyAll(SongAdded);
}
/*---------------------------------------------------------------------------------------
--	FUNCTION:	Playlist::removeSong
--
--	DATE:			March 22, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Brad Paugh
--
--	PROGRAMMER:		Brad Paugh
--
--      INTERFACE:      int removeSong(string filename)
--						string filename - the filename of the song to remove
--
--	RETURNS:		int: -1 on fail 0 on success	
--
--	NOTES:
--		removes a song from the playlist
---------------------------------------------------------------------------------------*/
int Playlist::removeSong(string filename)
{
	list<Song>::iterator it;

	for (it = playlist_.begin(); it != playlist_.end(); it++)
	{
		if ((*it).getFilename() == filename)
		{
			playlist_.erase(it);
			notifyAll(SongRemoved);

			return 0;
		}
	}

	return -1;
}
/*---------------------------------------------------------------------------------------
--	FUNCTION:	Playlist::voteSong
--
--	DATE:			March 22, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Brad Paugh
--
--	PROGRAMMER:		Brad Paugh
--
--      INTERFACE:      int removeSong(string filename)
--				string filename - the filename of the song to vote for
--
--	RETURNS:		int: -1 on fail 0 on success	
--
--	NOTES:
--		votes for a song in the playlist
---------------------------------------------------------------------------------------*/
int Playlist::voteSong(string filename)
{
	list<Song>::iterator it;
	for (it = playlist_.begin(); it != playlist_.end(); it++)
	{
		if ((*it).getFilename() == filename)
		{
			(*it).vote();
			return 0;
		}
	}
	return -1;
}

/*---------------------------------------------------------------------------------------
--	FUNCTION:	Playlist::getMostVotesSong
--
--	DATE:			March 22, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Brad Paugh
--
--      PROGRAMMER:		Brad Paugh
--
--      INTERFACE:      string getMostVotesSong()
--
--	RETURNS:		string - filename of the song with the most votes
--
--	NOTES:
--		This song will return the song with the most votes in the playlist. If there are songs
--		that have equal numbers of votes it will return the first one found.
---------------------------------------------------------------------------------------*/
string Playlist::getMostVotesSong()
{
	int highest = -1;
	string highestFilename = "No Song Found";
	list<Song>::iterator it;
	for (it = playlist_.begin(); it != playlist_.end(); it++)
	{
		if ((*it).getVotes() > highest)
		{
			highestFilename = (*it).getFilename();
		}
	}
	return highestFilename;
}

/*---------------------------------------------------------------------------------------
--	FUNCTION:	Playlist::setPlaying
--
--	DATE:			March 22, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Brad Paugh
--
--	PROGRAMMER:		Brad Paugh
--
--      INTERFACE:      void setPlaying(Song s)
--						Song s - the currently playing song
--
--	RETURNS:		void	
--
--	NOTES:
--		sets the currently playing song.
---------------------------------------------------------------------------------------*/
void Playlist::setPlaying(Song s)
{
	playing_ = s;
}

/*---------------------------------------------------------------------------------------
--	FUNCTION:	Playlist::getPlaying
--
--	DATE:			March 22, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Brad Paugh
--
--	PROGRAMMER:		Brad Paugh
--
--  INTERFACE:      Song getPlaying()
--
--	RETURNS:    Song - The currently playing song
--
--	NOTES:
--		gets the currently playing song.
---------------------------------------------------------------------------------------*/
Song Playlist::getPlaying()
{
	return playing_;
}

/*---------------------------------------------------------------------------------------
--	FUNCTION:	Playlist::copySongList
--
--	DATE:			March 27, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Erick Ribeiro
--
--	PROGRAMMER:		Erick Ribeiro
--
--      INTERFACE:      std::list<Song> copySongList ()
--
--	RETURNS:        std::list<Song> - List of Song objects.
--
--	NOTES:
--		gets the currently playing song.
---------------------------------------------------------------------------------------*/
std::list<Song> Playlist::copySongList ()
{
    return playlist_;
}

/*---------------------------------------------------------------------------------------
--	FUNCTION:	Playlist::parseToString
--
--	DATE:			March 22, 2010
--
--	REVISIONS		(Date and Description):
--
--	DESIGNER:		Brad Paugh
--
--	PROGRAMMER:		Brad Paugh
--
--      INTERFACE:      string parseToString()
--
--	RETURNS:		string - parsed string of all songs/votes
--
--	NOTES:
--		parses all the songs and votes in the playlist into a single string delimited by
--		the ETX character '\3'. This is so we can send the data across to the client.
--		returned string/packet will have the form
--
--		FILENAME\3VOTES\3FILENAME\3VOTES\3....and so on
---------------------------------------------------------------------------------------*/
string Playlist::parseToString()
{
	string listString = "";
	string s;
	ostringstream oss;
	list<Song>::iterator it;
	for (it = playlist_.begin(); it != playlist_.end(); it++)
	{
		//delimited by ETX (\3)
		oss << (*it).getFilename() << "\3" << (*it).getVotes() << "\3";
		listString.append(oss.str());
		//clear string stream
		oss.str(string());
	}
	return listString;
}
